﻿Imports System.Windows.Controls.Primitives
Imports System.ComponentModel

Public Class EnvelopePhaseDuration
    Inherits Thumb

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(EnvelopePhaseDuration), New FrameworkPropertyMetadata(GetType(EnvelopePhaseDuration)))
    End Sub



    <Category("FMMatrix")> _
    Public Property Value As Double
        Get
            Return DirectCast(GetValue(ValueProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(ValueProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register( _
        "Value", GetType(Double), GetType(EnvelopePhaseDuration),
            New FrameworkPropertyMetadata(1.0))



    <Category("FMMatrix")> _
    Public Property HZoom As Double
        Get
            Return DirectCast(GetValue(HZoomProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(HZoomProperty, value)
        End Set
    End Property

    Public Shared ReadOnly HZoomProperty As DependencyProperty = DependencyProperty.Register( _
        "HZoom", GetType(Double), GetType(EnvelopePhaseDuration),
            New FrameworkPropertyMetadata(1.0))


    Private Const MinDuration = 0.001953125

    Private origValue As Double
    Private origX As Double

    Private Sub EnvelopePhaseDuration_DragStarted(sender As Object, e As DragStartedEventArgs) Handles Me.DragStarted
        origValue = Value
        origX = PointToScreen(Mouse.GetPosition(Me)).X
    End Sub

    Private Sub EnvelopePhaseDuration_DragDelta(sender As Object, e As DragDeltaEventArgs) Handles Me.DragDelta
        Dim newX = PointToScreen(Mouse.GetPosition(Me)).X
        Dim _value = (newX - origX) / Math.Pow(2, HZoom) + origValue
        If _value < MinDuration Then _value = MinDuration
        If _value > 10 Then _value = 10
        Value = _value
    End Sub

End Class
